<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title></title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../js/main.js"></script>
<script language="javascript" type="text/javascript"
	src="../../My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">




function add() {

		var ss=document.getElementById("tid").innerHTML;
				if (ss == "存在") {
					//alert("cunzai");
					
					if (confirm("是否提交？")) {
		
		if(flag1==0){
			if (ckcouname() && ckcouaddress() && ckcoumoney()&&flag) {
					document.getElementById("form3").submit();	
					return true;
	}
		}
		 if(flag2==0){
			if (ckcouname() && ckcouaddress() && ckcoumoney()&&ckcoutime() && flag) {
			document.getElementById("form3").submit();	
			return true;}
		}
		
		
			/* alert(1);
				document.getElementById("form3").submit();		
		 */
			
	     }else{
	    return false;}
					
					
					return true;
					
				} else {
					//alert(2);
					return false;
					
				}
	
}
	var flag1=0;
	var flag2=0;
function stateChange(obj) {
		var table = document.getElementById("formTable");
		var objVal = obj.value;
		//alert(objVal);
		if (objVal == 0&&flag1==0) {
		//alert(obj);
		document.getElementById("o1").style.display="none";
		document.getElementById("o2").style.display="none";
		document.getElementById("o3").style.display="";
		document.getElementById("o4").style.display="";
		document.getElementById("state").selectedIndex=2;
		/*document.getElementById("o1").setAttribute("selected", "");
		document.getElementById("o3").setAttribute("selected", "selected");*/
		

		
			var timeInputs = document.getElementsByClassName("time");
			if (timeInputs != null) {
				while (true) {
					if (timeInputs.length <= 0) {
						break;
					}
					timeInputs[0].remove();
				}
			}
			var newElement = document.createElement("tr");
			var newTh = document.createElement("th");
			var newTd = document.createElement("td");
			newElement.className = "file";
			newTh.align = "center";
			newTh.innerHTML = "上传视频";
			newTd.className = "BGCgray";
			newTd.innerHTML = "<input type='file' name='myfile' />";
			newElement.appendChild(newTh);
			newElement.appendChild(newTd);
			table.appendChild(newElement);
			
		} else if (objVal == 1&&flag2==0) {
		//alert(obj);
			document.getElementById("o4").style.display="none";
			document.getElementById("o3").style.display="none";
			document.getElementById("o1").style.display="";
			document.getElementById("o2").style.display="";
			document.getElementById("state").selectedIndex=0;
			
			
			/*document.getElementById("o3").setAttribute("selected", "false");
			document.getElementById("o1").setAttribute("selected", "selected");*/
			var fileInputs = document.getElementsByClassName("file");
			if (fileInputs != null) {
				while (true) {
					if (fileInputs.length <= 0) {
						break;
					}
					fileInputs[0].remove();
				}
			}

			var newElement1 = document.createElement("tr");
			var newElement2 = document.createElement("tr");
			var newTh1 = document.createElement("th");
			var newTd1 = document.createElement("td");

			var newTh2 = document.createElement("th");
			var newTd2 = document.createElement("td");

			newElement1.className = "time";
			newElement2.className = "time";

			newTh1.align = "center";
			newTh2.align = "center";

			newTh1.innerHTML = "开课时间";
			newTh2.innerHTML = "结课时间";

			newTd1.className = "BGCgray";
			newTd2.className = "BGCgray";
			newTd1.innerHTML = "<input type='text' name='otime' id='otime' onclick='WdatePicker({dateFmt:'yyyy-MM-dd'}) class='Wdate' />";
			newTd2.innerHTML = "<input type='text' name='etime' id='etime' onClick='WdatePicker({dateFmt:'yyyy-MM-dd'}) class='Wdate'  />";

			newElement1.appendChild(newTh1);
			newElement1.appendChild(newTd1);

			newElement2.appendChild(newTh2);
			newElement2.appendChild(newTd2);

			table.appendChild(newElement1);
			table.appendChild(newElement2);
			
		}
		
}



	
	var xmlhttp;
	function a1(){
	var tname = document.getElementById("tcname").value;
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = rr;
	
		xmlhttp.open("get", "<%=request.getContextPath()%>/CourseServlet?param=selectTname&tname="+ tname + "&time=" + new Date().getTime(), true);
		xmlhttp.send(null);

	}

	var flag = false;
	function rr() {

		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {

				var ss = xmlhttp.responseText;
				document.getElementById("tid").innerHTML = ss;
				if (ss == "存在") {
					//alert("cunzai");
					flag = true;

				} else {
					//alert(2);
					flag = false;

				}
			}
		}
		return flag;
	}

	/* function ckall() {
		return ckcouname() && ckcouaddress() && ckcoumoney() && ckcoutime();
	} */

	function ckcouname() {

		var couname = document.getElementById("couname").value;
		if (couname.trim().length == 0) {
			document.getElementById("scouname").style.color = "red";
			document.getElementById("scouname").innerHTML = "课程名不能为空";
			return false;
		}
		document.getElementById("scouname").style.color = "green";
		document.getElementById("scouname").innerHTML = "√";
		return true;
	}

	function ckcouaddress() {
		var couaddress = document.getElementById("couaddress").value;
		if (couaddress.trim().length == 0) {
			document.getElementById("scouaddress").style.color = "red";
			document.getElementById("scouaddress").innerHTML = "地址不能为空";
			return false;
		}
		document.getElementById("scouaddress").style.color = "green";
		document.getElementById("scouaddress").innerHTML = "√";
		return true;
	}

	function ckcoumoney() {
		var coumoney = document.getElementById("coumoney").value;
		var mag = /^\d+$/;
		if (coumoney.trim().length == 0) {
			document.getElementById("scoumoney").style.color = "red";
			document.getElementById("scoumoney").innerHTML = "价格不能为空";
			return false;
		}
		if (!mag.test(coumoney)) {
			document.getElementById("scoumoney").style.color = "red";
			document.getElementById("scoumoney").innerHTML = "请输入数字,也不要输入空格";
			return false;
		}
		document.getElementById("scoumoney").style.color = "green";
		document.getElementById("scoumoney").innerHTML = "√";
		return true;
	}

	function ckcoutime() {
		var otime = document.getElementById("otime").value;
		var etime = document.getElementById("etime").value;
		if (otime < etime) {
			document.getElementById("setime").style.color = "green";
			document.getElementById("setime").innerHTML = "√";
			return true;
		} else {
			document.getElementById("setime").style.color = "red";
			document.getElementById("setime").innerHTML = "结课时间不能小于开课时间";
			return false;
		}

	}
</script>
</head>
<body>
	<table width="100%" height="100%" border="0" cellspacing="0"
		cellpadding="3">
		<tr>
			<td style="height:25px; background-color:#f3f3f3; font-weight:bold"
				valign="top">&nbsp;&nbsp;&nbsp;&nbsp;当前位置： <img
				src="../../images/arrow.gif" align="absmiddle">&nbsp;&nbsp;soda创业教育平台&nbsp;&nbsp;
				<img src="../../images/arrow.gif" align="absmiddle">&nbsp;&nbsp;后台管理&nbsp;&nbsp;
				<img src="../../images/arrow.gif" align="absmiddle">&nbsp;&nbsp;课程管理
			</td>
		</tr>
		<tr>
			<td
				style="height:34px; background-image:url(../../images/main_header.gif); border-bottom:1px solid #cfd8e0; padding:0px">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td width="20" height="34"><img
							src="../../images/main_headerL.gif" width="20" height="34"></td>
						<td style="color:#90c8e8;"><img
							src="../../images/icon_card.gif" width="16" height="16"
							align="absmiddle">&nbsp;&nbsp;<strong>添加课程</strong></td>
						<td align="right" class="white" style="padding-right:20px"><a
							href="javascript:history.go(-1);" class="barBtn"><img
								src="../../images/btn_left.gif" align="absmiddle"> 后退</a> <a
							href="javascript:history.go(+1);" class="barBtn"><img
								src="../../images/btn_right.gif" align="absmiddle"> 前进</a> <a
							href="javascript:window.location.reload();" class="barBtn"><img
								src="../../images/btn_refresh.gif" align="absmiddle"> 刷新</a></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td height="100%" valign="top">
				<div style="overflow:auto;height:100%; width:100%">
					<form id="form3"
						action="<%=basePath%>/CourseServlet?param=addCourse" method="post"
						enctype="multipart/form-data">
						<!-- onsubmit="return ckall()"  -->
						<table width="100%" id="formTable" border="0" cellpadding="3"
							cellspacing="1" class="table1">
							<TR>
								<th width="20%" align="center">课程名</th>
								<TD class="BGCgray"><input type="text" name="couname"
									id="couname" onblur="ckcouname()" /><span id="scouname"></span></TD>
							</TR>
							<TR>
								<th align="center">上课地点</th>
								<TD width="80%" class="BGCgray"><input type="text"
									name="couaddress" id="couaddress" onblur="ckcouaddress()" /><span
									id="scouaddress"></span></TD>
							</TR>
							<TR>
								<th align="center" id="coutype">课程类型</th>
								<TD class="BGCgray"><input type="radio" name="type"
									checked="checked" value="1" onclick="stateChange(this)"
									id="xianchang" />现场课程 <input type="radio" name="type"
									value="0" onclick="stateChange(this)" id="zaixian" />在线课程</TD>
							</TR>
							<TR>
								<th align="center">状态</th>
								<TD class="BGCgray"><select name="coustate" id="state">
										<option value="1" id="o1">可报名</option>
										<option value="2" id="o2">报名结束</option>
										<option value="3" id="o3" style="display:none;">可购买</option>
										<option value="4" id="o4" style="display:none;">已下架</option>
								</select></TD>
							</TR>
							<TR>
								<th align="center">讲师</th>
								<TD class="BGCgray"><input type="text" id="tcname"
									name="tname" onblur="a1()" /> <span id="tid"></span></TD>
							</TR>
							<TR>
								<th align="center">价格</th>
								<TD class="BGCgray"><input type="text" name="coumoney"
									id="coumoney" onblur="ckcoumoney()" /><span id="scoumoney"></span></TD>
							</TR>
							<TR class="time">
								<th align="center">开课时间</th>
								<TD class="BGCgray"><input name="otime" type="text"
									readonly="readonly"
									onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="Wdate"
									id="otime" /></TD>
							</TR>
							<TR class="time">
								<th align="center">预计结课时间</th>
								<TD class="BGCgray"><input name="etime" type="text"
									readonly="readonly"
									onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="Wdate"
									id="etime" onblur="ckcoutime()" /><span id="setime"></span></TD>
							</TR>
						</table>
					</form>
					<div style="width:100%; text-align:center; padding:5px">
						<BUTTON type="button" style="HEIGHT:25px" onClick="return add()">
							<img src="../..//images/btn_save.gif" width="16" height="16"
								align="absmiddle" /> 保存
						</BUTTON>
						<BUTTON type="button" style="HEIGHT:25px"
							onClick="javascript:history.go(-1);">
							<img src="../../images/btn_back.gif" width="16" height="16"
								align="absmiddle" /> 返回
						</BUTTON>
					</div>

				</div>
			</td>
		</tr>
	</table>
</body>
</html>